#include "lcd.h"
#include "lcd_init.h"
#include "pic.h"
#include "bsp_I2C.h"
#include "bsp_illum.h"
#include "bsp_DHT11.H"
#include "bsp_bluetooth.h"
#include "bsp_mois.h"
#include "bsp_RTC.h"
#include "bsp_Timer.h"
#include "Pump.h"
#include "Streering.h"
#include "Ventilator.h"

u8 status = 0;
u8 auto_fan = 0, f_flag = 0;
u8 auto_water = 0, w_flag = 0;
u16 half_illum = 0;
u16 auto_illum = 0, i_flag = 0;
u8 Pump_r = 0;
u8 Steering_r = 0;
u8 Ven_r = 0;

void sys_init()
{
    EAXSFR();
    bsp_I2C_init();
    Timer_init();
    bsp_illum_init();
    DHT11_init();
    bluetooth_init();
    Pump_init();
    Ven_init();
    Steering_init();
    mois_init();
    EA = 1;
}

void do_control()
{
    if (RX2_Buffer[0] != 0x53 || RX2_Buffer[1] != 0x54)
    {
        return;
    }
    if (RX2_Buffer[3] == '1')
    {
        Ven_r = 1;
    }
    else if (RX2_Buffer[3] == '2')
    {
        Ven_r = 0;
        auto_fan = 0;
    }
    else if (RX2_Buffer[3] == '3')
    {
        Steering_r = 2;
    }
    else if (RX2_Buffer[3] == '4')
    {
        Steering_r = 0;
        auto_illum = 0;
    }
    else if (RX2_Buffer[3] == '5')
    {
        Pump_r = 1;
    }
    else if (RX2_Buffer[3] == '6')
    {
        Pump_r = 0;
        auto_water = 0;
    }
    else if (RX2_Buffer[3] == '7')
    {
        Steering_r = 1;
    }
    else if (RX2_Buffer[3] == 'F')
    {
        if (RX2_Buffer[4] == 'X')
            auto_fan = 0;
        else
        {
            auto_fan = RX2_Buffer[4] - 26;
        }
    }
    else if (RX2_Buffer[3] == 'V')
    {
        if (RX2_Buffer[4] == 'X')
        {
            auto_illum = 0;
            half_illum = 0;
        }
        else
        {
            half_illum = (RX2_Buffer[4] - 48) * 250 + 500;
            auto_illum = (RX2_Buffer[5] - 48) * 250 + 500;
        }
    }
    else if (RX2_Buffer[3] == 'W')
    {
        if (RX2_Buffer[4] == 'X')
            auto_water = 0;

        else
        {
            auto_water = (RX2_Buffer[4] - 48) * 10 + 20;
        }
    }
    else if (RX2_Buffer[5] == 'M' && RX2_Buffer[6] == 'E')
    {
        clock.hour = (RX2_Buffer[7] - 48) * 10 + (RX2_Buffer[8] - 48);
        clock.minute = (RX2_Buffer[9] - 48) * 10 + (RX2_Buffer[10] - 48);
        clock.month = (RX2_Buffer[11] - 48) * 10 + (RX2_Buffer[12] - 48);
        clock.day = (RX2_Buffer[13] - 48) * 10 + (RX2_Buffer[14] - 48);
        RTC_SetTime(&clock);
    }
}

void main()
{
    u8 str[15], last_pum, last_v, last_s;
    static u8 num = 0, humidity;
    static float temperature;
    static int Siol_H = 0;
    static u16 i_num = 0;
    sys_init();
    LCD_Init();
    LCD_Fill(0, 0, LCD_W, LCD_H, WHITE);
    LCD_Fill(0, 0, LCD_W, 60, LGRAY);
    LCD_Fill(0, 270, LCD_W, LCD_H, LGRAY);
    LCD_ShowChinese(24, 30, "智慧农业控制系统", BLACK, LGRAY, 24, 0);
    clock.year = 2023;
    clock.month = 12;
    clock.day = 2;
    clock.week = 6;
    clock.hour = 11;
    clock.minute = 48;
    clock.second = 0;
    RTC_SetTime(&clock);
    while (1)
    {
        if (t_20ms == 1)
        {
            t_20ms = 0;
            last_pum = Pump_r;
            last_v = Ven_r;
            last_s = Steering_r;
            bluetooth_get_data();
            if (last_pum != Pump_r)
            {
                Pump_run(Pump_r);
            }
            if (last_v != Ven_r)
            {
                Ven_run(Ven_r);
            }
            if (last_s != Steering_r)
            {
                Steering_run(Steering_r);
            }
        }
        if (start_DHT11 == 1)
        {
            start_DHT11 = 0;
            DHT11_get_temperature(&humidity, &temperature);
            RTC_ReadTime(&clock);
            bsp_illum_get_data();
            i_num = illum_num;
            sprintf(str, "Air-H: %d%%", (int)humidity);
            LCD_ShowString(10, 60, str, BLACK, WHITE, 24, 0);
            sprintf(str, "Air-T: %.2f      \0", temperature);
            LCD_ShowString(10, 90, str, BLACK, WHITE, 24, 0);
            sprintf(str, "illum: %d Lx     \0", (int)i_num);
            LCD_ShowString(10, 120, str, BLACK, WHITE, 24, 0);
            Siol_H = mois_get();
            sprintf(str, "Soil-H: %d%%", Siol_H);
            LCD_ShowString(10, 150, str, BLACK, WHITE, 24, 0);
            sprintf(str, "%02d-%02d  %02d:%02d:%02d", (int)clock.month, (int)clock.day, (int)clock.hour, (int)clock.minute, (int)clock.second);
            LCD_ShowString(24, 270, str, BLACK, LGRAY, 24, 0);
            if (Ven_r == 0)
            {
                LCD_Fill(40, 180, 100, 240, WHITE);
            }
            else
            {
                LCD_ShowPicture(40, 180, 60, 60, gImage_1);
            }
            if (Pump_r == 0)
            {
                LCD_Fill(120, 180, 180, 240, WHITE);
            }
            else
            {
                LCD_ShowPicture(120, 180, 60, 60, gImage_2);
            }
            num++;
            if (f_flag != 0 && f_flag <= 10)
            {
                f_flag++;
            }
            else if (f_flag > 10)
            {
                f_flag = 0;
                Ven_run(0);
            }
            if (w_flag != 0 && w_flag <= 10)
            {
                w_flag++;
            }
            else if (w_flag > 10)
            {
                w_flag = 0;
                Pump_run(0);
            }
            if (i_flag != 0 && i_flag <= 10)
            {
                i_flag++;
            }
            else
            {
                i_flag = 0;
            }
        }
        if (num == 5)
        {
            printf("温度: %.1f°C,湿度: %d%%,光照:%dLx, 土壤:  %d%%", temperature, (int)humidity, (int)i_num, (int)Siol_H);
            num = 0;
        }

        if (auto_fan != 0)
        {
            if (temperature >= auto_fan && f_flag == 0)
            {
                f_flag = 1;
                printf("温度：%.1f°C,自动通风开启", temperature);
                Ven_r = 1;
                Ven_run(1);
            }
        }
        if (auto_water != 0)
        {
            if (Siol_H <= auto_water && w_flag == 0)
            {
                w_flag = 1;
                Pump_r = 1;
                Pump_run(1);
                printf("土壤湿度：%d%%，自动浇水开启", (int)Siol_H);
            }
        }
        if (auto_illum != 0)
        {
            if (i_flag == 0)
            {
                i_flag = 1;
                if (illum_num <= half_illum)
                {
                    Steering_run(0);
                }
                else if (illum_num <= auto_illum)
                {
                    Steering_run(1);
                }
                else
                {
                    Steering_run(2);
                }
            }
        }
    }
}
